<?php

namespace app\adminapi\controller;

use app\common\model\Admin;
use app\common\model\Auth as ModelAuth;
use app\common\model\Role;
use think\Request;

class Auth extends BaseApi
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $param = input();
        $where = [];
        if (!empty($param['keyword'])) {
            $where['auth_name'] = ['like', '%' . $param['keyword'] . '%'];
        }
        $list = ModelAuth::where($where)->select();
        $list = collection($list)->toArray();
        if (!empty($param['type']) && $param['type'] == 'tree') {
            $list = get_tree_list($list);
        } else {
            $list = get_cate_list($list);
        }
        $this->ok($list);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //获取参数
        $params = input();
        //校验
        $validate = $this->validate($params, [
            'auth_name|权限名' => 'require',
            'pid|父级权限' => 'require',
            // 'auth_c|控制器名' => 'require',
            // 'auth_a|方法名' => 'require',
            'is_nav|是否菜单' => 'require',
            // 'level' => 'require',
        ]);
        if ($validate !== true) {
            $this->fail($validate);
        }
        //组装数据
        if ($params['pid'] == 0) {
            //顶级权限
            $params['pid_path'] = 0;
            $params['level'] = 0;
            $params['auth_c'] = '';
            $params['auth_a'] = '';
        } else {
            $p_info = ModelAuth::find($params['pid']);
            if (empty($p_info)) {
                $this->fail('父级权限不存在');
            }
            $params['pid_path'] = $p_info['pid_path'] . '_' . $p_info['id'];
            $params['level'] = $p_info['level'] + 1;
        }

        $auth = ModelAuth::create($params, true);
        $info = ModelAuth::find($auth['id']);
        $this->ok($info);
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        $auth = ModelAuth::field('id,auth_name,pid,pid_path,auth_c,auth_a,is_nav,level')->find($id);
        $this->ok($auth);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $params = input();

        if (empty($params['pid'])) {
            $params['pid'] = 0;
        }
        if (empty($params['is_nav'])) {
            $params['is_nav'] = $params['radio'];
        }

        $validate = $this->validate($params, [
            'auth_name|权限名' => 'require',
            'pid|父级权限' => 'require',
            'is_nav|是否菜单' => 'require',
        ]);
        if ($validate !== true) {
            $this->fail($validate, 401);
        }

        $auth = ModelAuth::find($id);
        if (empty($auth)) {
            $this->fail('数据异常，修改的权限不存在');
        }

        if ($params['pid'] == 0) {
            $params['pid_path'] = 0;
            $params['level'] = 0;
        } else if ($params['pid'] != $auth['pid']) {
            $p_auth = ModelAuth::find($params['pid']);
            if (empty($p_auth)) {
                $this->fail('数据异常，父级权限不存在');
            }
            $params['pid_path'] = $p_auth['pid_path'] . '_' . $p_auth['id'];
            $params['level'] = $p_auth['level'] + 1;
        }
        ModelAuth::update($params, ['id' => $id], true);

        $info = ModelAuth::find($id);
        $this->ok($info);
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $total = ModelAuth::where(['pid' => $id])->count();
        if ($total > 0) {
            $this->fail('该权限下有子权限，不能删除');
        }
        ModelAuth::destroy($id);
        $this->ok();
    }

    public function nav()
    {
        $userId = input('user_id');
        $userInfo = Admin::find($userId);
        $roleId = $userInfo['role_id'];
        if ($roleId == 1) {
            $list = ModelAuth::where(['is_nav' => 1])->select();
        } else {
            $roleInfo = Role::find($roleId);
            $list = ModelAuth::where(['is_nav' => 1])->where('id', 'in', $roleInfo['role_auth_ids'])->select();
        }
        // 将数据转换为数组
        $list = collection($list)->toArray();
        $list = get_tree_list($list);
        $this->ok($list);
    }
}
